﻿using System;
using System.Xml;
using System.Collections.ObjectModel;

namespace Builderator.Core.Components
{
    public class ServerList : ObservableCollection<string>
    {
        public ServerList()
            : base()
        {

        }

        public ServerList(string settings)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(settings);

            XmlElement rootElement = xmlDoc.DocumentElement;

            foreach (XmlElement child in rootElement.ChildNodes)
            {
                this.Add(child.GetAttribute("Name"));
            }
        }

        public override string ToString()
        {
            XmlDocument xmlDoc = new XmlDocument();

            XmlElement childElement;
            XmlElement rootElement = xmlDoc.CreateElement("ServerList");

            foreach (string server in this)
            {
                childElement = xmlDoc.CreateElement("Server");

                childElement.SetAttribute("Name", server);

                rootElement.AppendChild(childElement);
            }

            xmlDoc.AppendChild(rootElement);

            return xmlDoc.InnerXml;
        }
    }
}
